package com.javaweb.admin.controller;

import com.javaweb.admin.entity.*;
import com.javaweb.admin.mapper.*;
import com.javaweb.admin.query.*;
import com.javaweb.admin.service.IKDLCustomerService;
import com.javaweb.admin.service.IKDLMaterialService;
import com.javaweb.admin.service.IKDLSupplierService;
import com.javaweb.admin.vo.transreport.KdlTransReportVo;
import com.javaweb.common.common.BaseController;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.utils.*;
import com.javaweb.system.utils.ShiroUtils;
import com.seegw.shaphar.IKDLService;
import com.seegw.shaphar.INCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/kdl")
public class KDLController extends BaseController {

    @Autowired
    private IKDLService kdlService;

    @Autowired
    private INCService ncService;
    @Autowired
    IKDLCustomerService kdlCustomerService;

    @Autowired
    IKDLSupplierService kdlSupplierService;
    @Autowired
    IKDLMaterialService kdlMaterialService;
    @Autowired
    KdlRGRMapper kdlRGRMapper;
    @Autowired
    KdlPODMapper kdlPODMapper;
    @Autowired
    KdlQffFeedbackMapper kdlQffFeedbackMapper;
    @Autowired
    KdlMaintainReportMapper kdlMaintainReportMapper;
    @Autowired
    KdlTransReportMapper kdlTransReportMapper;
    @Autowired
    KdlMaraMapper kdlMaraMapper;


    @PostMapping("ncwe")
    public JsonResult ncwe()
    {
        return ncService.submitGspUnQualifiedRecord(1);
    }

    @PostMapping("/kdlchg")
    public JsonResult kdlchg(RGRQuery query) {

        if (StringUtils.isEmpty(query.getPrincipalid())) {
            query.setPrincipalid(CommonConfig.kdlCompanyId);
        }

        query.setDoctype("CHG");
        query.setEndtime(DateUtils.getTime());

        query.setStarttime(kdlService.getCHGMaxSyncTime());

        return kdlService.syncCHG(query);


    }
    @GetMapping("/kdlchglist")
    public JsonResult kdlchglist(KDLchgQuery query) {
        return kdlService.getKdlchgList(query);
    }
    @GetMapping("/index")
    public JsonResult index(KDLCustomerQuery query) {
        return kdlCustomerService.getCustomerList(query);
    }

    @GetMapping("/regionlist")
    public JsonResult regionlist() {
        return kdlService.getRegionList();
    }


    @GetMapping("/ncgeneralout")
    public JsonResult ncgeneralout(NCGeneralOutQuery query) {
        return kdlService.getNCGeneralOutList(query);
    }

    @GetMapping("/ncgeneraloutdetail")
    public JsonResult ncgeneraloutdetail(NCGeneralOutQuery query) {
        return kdlService.getNCGeneralOutDetailList(query.getCgeneralhid());
    }

    @GetMapping("/ncgeneralin")
    public JsonResult ncgeneralin(NCGeneralOutQuery query) {
        return kdlService.getNCGeneralInList(query);
    }

    @GetMapping("/ncgeneralindetail")
    public JsonResult ncgeneralindetail(NCGeneralOutQuery query) {
        return kdlService.getNCGeneralInDetailList(query.getCgeneralhid());
    }

    @GetMapping("/ncpoorder")
    public JsonResult ncpoorder(NCPOOrderQuery query) {
        //根据billtype获取字典中配置的单据类型
        if (!StringUtils.isEmpty(query.getBilltype())) {
            query.setBilltypelist(kdlService.getBillType(query.getBilltype()));
            query.setOrglist(ShiroUtils.getOrgCode());
            return kdlService.getNCPOOrderList(query);
        } else {
            return JsonResult.success();
        }

    }

    @GetMapping("/ncbilltype")
    public JsonResult ncbilltype(NCBilltypeQuery query) {
        return kdlService.getNCBilltypeList(query.getOrgcode(), query.getBill());
    }

    @GetMapping("/ncoutbilltype")
    public JsonResult ncoutbilltype(NCBilltypeQuery query) {
        return kdlService.getGeneralOutBillType();
    }

    @GetMapping("/ncinbilltype")
    public JsonResult ncinbilltype(NCBilltypeQuery query) {
        return kdlService.getGeneralInBillType();
    }

    @GetMapping("/ncpoorderdetail")
    public JsonResult ncpoorderdetail(NCPOOrderQuery query) {
        return kdlService.getNCPOOrderDetailList(query.getPkOrder());
    }

    @GetMapping("/ncporejectdetail")
    public JsonResult ncporejectdetail(NCPOOrderQuery query) {
        return kdlService.getRejectDetail(query.getVbillcode());
    }

    @GetMapping("/zbfedetail/{id}")
    public JsonResult zbfedetail(@PathVariable("id") Integer id) {
        return kdlService.getZBFEDetail(id);
    }


    @GetMapping("/ncpoinspectdetail")
    public JsonResult ncpoinspectdetail(NCPOOrderQuery query) {
        return kdlService.getPurchaseinInspectionDetail(query.getVbillcode());
    }


    @GetMapping("/ncsoorder")
    public JsonResult ncsoorder(NCSOOrderQuery query) {
        if (!StringUtils.isEmpty(query.getBilltype())) {
            query.setBilltypelist(kdlService.getBillType(query.getBilltype()));
            query.setOrglist(ShiroUtils.getOrgCode());
            return kdlService.getNCSOOrderList(query);
        } else {
            return JsonResult.success();
        }
    }

    @GetMapping("/ncsoorderdetail")
    public JsonResult ncsoorderdetail(NCSOOrderQuery query) {
        return kdlService.getNCSOOrderDetailList(query.getPkOrder());
    }

    @GetMapping("/supplierindex")
    public JsonResult supplierindex(KDLSupplierQuery query) {
        return kdlSupplierService.getSupplierList(query);

    }

    @GetMapping("/materialindex")
    public JsonResult materialindex(KDLMaterialQuery query) {
        return kdlMaterialService.getMaterialList(query);
    }

    @GetMapping("/materialgroup")
    public JsonResult materialgroup() {
        return kdlMaterialService.getMaterialGroupList();
    }

    @GetMapping("/nconhandlist")
    public JsonResult nconhandlist(KdlNcOnhandQuery query) {
        return kdlService.getNCOnHandList(query);
    }

    @GetMapping("/plantlist")
    public JsonResult plantlist() {
        return JsonResult.success(kdlService.getPlantList());
    }

    @GetMapping("/bincodelist/{plant}")
    public JsonResult bincodelist(@PathVariable("plant") String plant) {
        return JsonResult.success(kdlService.getBincodeList(plant));
    }

    @GetMapping("/inventorylist")
    public JsonResult inventorylist(KdlInventoryQuery query) {
        return kdlService.getKdlInventory(query);
    }

    @GetMapping("/qfffeedbacklist")
    public JsonResult qfffeedbacklist(KdlQffFeedbackQuery query) {
        return kdlService.getKdlQffFeedback(query);
    }

    @GetMapping("/transreportlist")
    public JsonResult transreportlist(KdlTransReportQuery query) {
        return kdlService.getKdlTransReport(query);
    }

    @GetMapping("/maintainreportlist")
    public JsonResult maintainreportlist(KdlMaintainReportQuery query) {
        return kdlService.getKdlMaintainReport(query);
    }

    @GetMapping("/rgrlist")
    public JsonResult rgrlist(KdlRGRQuery query) {
        return kdlService.getKdlRGR(query);
    }

    @GetMapping("/zbfelist")
    public JsonResult zbfelist(KdlRGRQuery query) {
        return kdlService.getKdlZBFE(query);
    }


    @GetMapping("/podlist")
    public JsonResult podlist(KdlPODQuery query) {
        return kdlService.getKdlPOD(query);
    }

    @PostMapping("/getftpfile")
    public JsonResult getftpfile(KdlInventoryQuery query) {

        return kdlService.getFtpFile("000200067786_000000000739.doc", "temp");
    }


    @PostMapping("/syncinventory")
    public JsonResult syncinventory(KdlInventoryQuery query) {
        if (StringUtils.isEmpty(query.getPlant())) {
            query.setPlant("T150");
        }
        if (StringUtils.isEmpty(query.getPrincipalid())) {
            query.setPrincipalid(CommonConfig.kdlCompanyId);
        }

        query.setDoctype("Inventory");
        query.setMaterial("");
        query.setVendorbatch("");
        return kdlService.syncInventory(query);
    }

    @PostMapping("/syncmara")
    public JsonResult syncmara(@RequestBody KdlMaraQuery query) {
        if (StringUtils.isEmpty(query.getPrincipalid())) {
            query.setPrincipalid(CommonConfig.kdlCompanyId);
        }

        query.setDoctype("MARA");
        query.setMasterdatano("");
        query.setStarttime(kdlService.getMasterDataMaxSyncTime(query.getMastertype()));
        query.setEndtime(DateUtils.getTime());
        return kdlService.syncMasterDataResult(query);
    }

    @PostMapping("/synccoa")
    public JsonResult synccoa() {
        return kdlService.syncCoaFile();
    }

    @GetMapping("/maraindex")
    public JsonResult maraindex(KdlMaraQuery query) {
        return kdlService.getMaraList(query);
    }

    @GetMapping("/coaindex")
    public JsonResult coaindex(KdlCoaQuery query) {
        return kdlService.getCoaList(query);
    }

    @PostMapping("/syncqfffeedback")
    public JsonResult syncqfffeedback(QffFeedbackQuery query) {
        if (StringUtils.isEmpty(query.getPrincipalid())) {
            query.setPrincipalid(CommonConfig.kdlCompanyId);
        }

        query.setDoctype("QFF");
        query.setStarttime(kdlService.getQffMaxSyncTime());
        query.setEndtime(DateUtils.dateTime());
        return kdlService.syncQffFeedback(query);
    }

    @PostMapping("/synctransreport")
    public JsonResult synctransreport(QffFeedbackQuery query) {
        if (StringUtils.isEmpty(query.getPrincipalid())) {
            query.setPrincipalid(CommonConfig.kdlCompanyId);
        }

        query.setDoctype("ZTRN");
        query.setStarttime(kdlService.getTransReportMaxSyncTime());
        query.setEndtime(DateUtils.dateTime());
        return kdlService.syncTransReport(query);
    }

    @PostMapping("/syncmaintainreport")
    public JsonResult syncmaintainreport(QffFeedbackQuery query) {
        if (StringUtils.isEmpty(query.getPrincipalid())) {
            query.setPrincipalid(CommonConfig.kdlCompanyId);
        }

        query.setDoctype("INST");
        query.setStarttime(kdlService.getMaintainReportMaxSyncTime());
        query.setEndtime(DateUtils.dateTime());
        return kdlService.syncMaintainReport(query);
    }

    @PostMapping("/synczbfe")
    public JsonResult synczbfe(RGRQuery query) {
        if (StringUtils.isEmpty(query.getPrincipalid())) {
            query.setPrincipalid(CommonConfig.kdlCompanyId);
        }

        query.setDoctype("ZBFE");
        query.setStarttime(kdlService.getZBFEMaxSyncTime());
        query.setEndtime(DateUtils.getTime());
        return kdlService.syncZBFE(query);
    }

    @PostMapping("/syncrgr")
    public JsonResult syncrgr(RGRQuery query) {
        if (StringUtils.isEmpty(query.getPrincipalid())) {
            query.setPrincipalid(CommonConfig.kdlCompanyId);
        }

        query.setDoctype("RGR");
        query.setStarttime(kdlService.getRGRMaxSyncTime());
        query.setEndtime(DateUtils.getTime());
        return kdlService.syncRGR(query);
    }

    @PostMapping("/syncpod")
    public JsonResult syncpod(RGRQuery query) {
        if (StringUtils.isEmpty(query.getPrincipalid())) {
            query.setPrincipalid(CommonConfig.kdlCompanyId);
        }

        query.setDoctype("POD");
        query.setStarttime(kdlService.getPODMaxSyncTime());
        query.setEndtime(DateUtils.getTime());
        return kdlService.syncPOD(query);
    }

    @PostMapping("/synccustomer")
    public JsonResult synccustomer() {
        return kdlService.syncCustomer();
    }

    @PostMapping("/syncsupplier")
    public JsonResult syncsupplier() {
        return kdlService.syncSupplier();
    }

    @PostMapping("/syncmaterial")
    public JsonResult syncmaterial() {
        return kdlService.syncMaterial();
    }

    @PostMapping("/submitInspect/{id}")
    public JsonResult submitInspect(@PathVariable("id") Integer id) {
        return kdlService.submitInspectToNC(id, true);
    }

    @PostMapping("/submitReturn/{id}")
    public JsonResult submitReturn(@PathVariable("id") Integer id) {
        return kdlService.submitPurchaseReturnToNC(id);
    }

    @PostMapping("/submitSaleReturn/{id}")
    public JsonResult submitSaleReturn(@PathVariable("id") Integer id) {
        return kdlService.submitSaleReturnToNC(id);
    }

    @PostMapping("/submitSaleOutToNC/{id}")
    public JsonResult submitSaleOutToNC(@PathVariable("id") Integer id) {
        return kdlService.submitSaleOutToNC(id);
    }

    @PostMapping("/submitSaleOutQCToNC/{vendorso}")
    public JsonResult submitSaleOutQCToNC(@PathVariable("vendorso") String vendorso) {
        return kdlService.submitSaleOutQCToNC(vendorso);
    }

    @PostMapping("/submitWhstrans/{id}")
    public JsonResult submitWhstrans(@PathVariable("id") String id) {
        return kdlService.submitWhstransToNC(id);
    }

    /**
     * 退货回来的移库
     * @param id
     * @return
     */
    @PostMapping("/submitWhstransT/{id}")
    public JsonResult submitWhstransT(@PathVariable("id") String id) {
        return kdlService.submitWhstransToNCT(id,true);
    }

    @PostMapping("/submitGeneralOut/{id}")
    public JsonResult submitGeneralOut(@PathVariable("id") String id) {
        return kdlService.submitGeneralOutToNC(id, true);
    }

    @PostMapping("/submitGeneralIn/{id}")
    public JsonResult submitGeneralIn(@PathVariable("id") String id) {
        return kdlService.submitGeneralInToNC(id, false);
    }


    @PostMapping("/uploadmovement/{id}")
    public JsonResult uploadmovement(@PathVariable("id") Integer id) {
        return kdlService.uploadGoodsmovement(id);
    }


    @PostMapping("/uploadoutbound/{id}")
    public JsonResult uploadoutbound(@PathVariable("id") Integer id) {
        return kdlService.uploadOutbound(id);
    }

    @PostMapping("/uploadfeedback/{id}")
    public JsonResult uploadfeedback(@PathVariable("id") Integer id) {
        return kdlService.uploadExceptionFeedback(id);
    }


    @PostMapping("/savefeedback")
    public JsonResult savefeedback(@RequestBody KdlQffFeedback entity) {
        return kdlService.saveFeedback(entity);
    }

    @PostMapping("/savetransoper")
    public JsonResult savetransoper(@RequestBody KdlTransReport entity) {
        return kdlService.saveTransOperator(entity);
    }


    @PostMapping("/customer/{id}")
    public JsonResult customer(@PathVariable("id") Integer id) {
        return kdlService.submitCustomer(id);
    }

    @PostMapping("/savecustomer")
    public JsonResult savecustomer(@RequestBody KDLCustomer entity) {
        return kdlCustomerService.saveCustomer(entity);
    }

    @PostMapping("/supplier/{id}")
    public JsonResult supplier(@PathVariable("id") Integer id) {
        return kdlService.submitSupplier(id);
    }

    @PostMapping("/material/{id}")
    public JsonResult material(@PathVariable("id") Integer id) {
        return kdlService.submitMaterial(id);
    }

    @GetMapping("/customerlog")
    public JsonResult customerlog(KDLCustomerQuery query) {
        return JsonResult.success(kdlCustomerService.getNCCustomerLogList(query.getCustcode()));
    }

    @GetMapping("/supplierlog")
    public JsonResult supplierlog(KDLSupplierQuery query) {
        return JsonResult.success(kdlSupplierService.getNCSupplierLogList(query.getSuppliercode()));
    }

    @GetMapping("/materiallog")
    public JsonResult materiallog(KDLMaterialQuery query) {
        return JsonResult.success(kdlMaterialService.getNCMaterialLogList(query.getMaterialcode()));
    }

    @GetMapping("/feedbackattach")
    public JsonResult feedbackattach(KdlQffFeedbackQuery query) {
        return kdlService.getFeedbackAttachList(query.getQffno());
    }

    @GetMapping("/qcuserlist/{dataType}")
    public JsonResult qcuserlist(@PathVariable("dataType") String dataType) {
        return kdlService.getQCUserList(dataType);
    }

    @PostMapping("/manualsync")
    public JsonResult manualsync(@RequestBody KdlManualSync entity) {
        return kdlService.manualsync(entity);
    }


}
